package com.white.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.white.bean.Classify;
import com.white.service.ClassifyService;
import com.white.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ClassifyController {
    @Autowired
    ClassifyService classifyService;

    @Autowired
    Utils utils;

    @GetMapping("/getClassify")
    @ResponseBody
    public List<Classify> getClassify() {
        return classifyService.list();
    }

    @GetMapping("/classify")
    public String classify() {
        return "classify";
    }


    @GetMapping("/getClassifyPage/{page}/{limit}")
    @ResponseBody
    public Object getClassifyPage(@PathVariable("page") int page,
                              @PathVariable("limit") int limit) {
        Page<Classify> classifyPage = new Page<>(page, limit);
        return classifyService.page(classifyPage, null);
    }

    @GetMapping("/deleteClassifyById/{id}")
    @ResponseBody
    public void deleteClassifyById(@PathVariable("id") int id) {
        classifyService.removeById(id);
    }

    @PostMapping("/updateClassify")
    @ResponseBody
    public void updateClassify(@RequestParam("item") String item) {
        Classify classify = utils.jsonToEntry(Classify.class, item);
        classifyService.updateById(classify);
    }
}
